package hu.ikti.oamadmin.types.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "VERSION")
@SequenceGenerator(sequenceName = "VERSION_SEQ", name = "VER_SEQ", allocationSize = 1)
public class Version implements Comparable<Object>, Serializable {

	private static final long serialVersionUID = 1L;

	private Long id;
	private int major;
	private int minor;
	private int micro;
	private String qualifier = "";

	private static final String SEPARATOR = ".";

	public Version() {
	}

	public Version(int major, int minor, int micro, String qualifier) {
		if (qualifier == null) {
			qualifier = "";
		}

		this.major = major;
		this.minor = minor;
		this.micro = micro;
		this.qualifier = qualifier;
	}

	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "VER_SEQ")
	public Long getId() {
		return id;
	}

	@Column(name = "MAJOR", nullable = false)
	public int getMajor() {
		return major;
	}

	@Column(name = "MINOR", nullable = false)
	public int getMinor() {
		return minor;
	}

	@Column(name = "MICRO", nullable = false)
	public int getMicro() {
		return micro;
	}

	@Column(name = "QUALIFIER", nullable = true)
	public String getQualifier() {
		return qualifier;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setQualifier(String qualifier) {
		if (qualifier == null) {
			qualifier = "";
		} else {
			this.qualifier = qualifier;
		}
	}

	public void setMajor(int major) {
		this.major = major;
	}

	public void setMinor(int minor) {
		this.minor = minor;
	}

	public void setMicro(int micro) {
		this.micro = micro;
	}

	public String toString() {
		int q = qualifier.length();
		StringBuffer result = new StringBuffer(20 + q);
		result.append(major);
		result.append(SEPARATOR);
		result.append(minor);
		result.append(SEPARATOR);
		result.append(micro);
		if (q > 0) {
			result.append(SEPARATOR);
			result.append(qualifier);
		}
		return result.toString();
	}

	public int hashCode() {
		return (major << 24) + (minor << 16) + (micro << 8)
				+ qualifier.hashCode();
	}

	public boolean equals(Object object) {
		if (object == this) { // quicktest
			return true;
		}

		if (!(object instanceof Version)) {
			return false;
		}

		Version other = (Version) object;
		return (major == other.major) && (minor == other.minor)
				&& (micro == other.micro) && qualifier.equals(other.qualifier);
	}

	public int compareTo(Object object) {
		if (object == this) { // quicktest
			return 0;
		}

		Version other = (Version) object;

		int result = major - other.major;
		if (result != 0) {
			return result;
		}

		result = minor - other.minor;
		if (result != 0) {
			return result;
		}

		result = micro - other.micro;
		if (result != 0) {
			return result;
		}

		return qualifier.compareTo(other.qualifier);
	}

}
